package com.puti.doctor.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.puti.common.core.constant.HttpStatus;
import com.puti.common.core.domain.R;
import com.puti.doctor.domian.Cart;
import com.puti.doctor.domian.GoodsInfo;
import com.puti.doctor.domian.GoodsSeckill;
import com.puti.doctor.domian.PercentagelVo;
import com.puti.doctor.mapper.GoodsSeckillMapper;
import com.puti.doctor.service.IGoodsSeckillService;
import com.puti.doctor.domian.PatientInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 商品-秒杀 商品-秒杀Service业务层处理
 *
 * @author puti
 * @date 2021-04-21
 */
@Service
public class GoodsSeckillServiceImpl implements IGoodsSeckillService
{
    @Autowired
    private GoodsSeckillMapper goodsSeckillMapper;

    /**
     * 查询商品-秒杀 商品-秒杀
     *
     * @param seckillId 商品-秒杀 商品-秒杀ID
     * @return 商品-秒杀 商品-秒杀
     */
    @Override
    public GoodsSeckill selectGoodsSeckillById(Long seckillId)
    {
        return goodsSeckillMapper.selectGoodsSeckillById(seckillId);
    }

    /**
     * 查询商品-秒杀 商品-秒杀列表
     *
     * @param goodsSeckill 商品-秒杀 商品-秒杀
     * @return 商品-秒杀 商品-秒杀
     */
    @Override
    public List<GoodsSeckill> selectGoodsSeckillList(GoodsSeckill goodsSeckill)
    {
        return goodsSeckillMapper.selectGoodsSeckillList(goodsSeckill);
    }

    /**
     * 新增商品-秒杀 商品-秒杀
     *
     * @param goodsSeckill 商品-秒杀 商品-秒杀
     * @return 结果
     */
    @Override
    public int insertGoodsSeckill(GoodsSeckill goodsSeckill)
    {
        return goodsSeckillMapper.insertGoodsSeckill(goodsSeckill);
    }

    /**
     * 修改商品-秒杀 商品-秒杀
     *
     * @param goodsSeckill 商品-秒杀 商品-秒杀
     * @return 结果
     */
    @Override
    public int updateGoodsSeckill(GoodsSeckill goodsSeckill)
    {
        return goodsSeckillMapper.updateGoodsSeckill(goodsSeckill);
    }

    /**
     * 批量删除商品-秒杀 商品-秒杀
     *
     * @param seckillIds 需要删除的商品-秒杀 商品-秒杀ID
     * @return 结果
     */
    @Override
    public int deleteGoodsSeckillByIds(Long[] seckillIds)
    {
        return goodsSeckillMapper.deleteGoodsSeckillByIds(seckillIds);
    }

    /**
     * 删除商品-秒杀 商品-秒杀信息
     *
     * @param seckillId 商品-秒杀 商品-秒杀ID
     * @return 结果
     */
    @Override
    public int deleteGoodsSeckillById(Long seckillId)
    {
        return goodsSeckillMapper.deleteGoodsSeckillById(seckillId);
    }



    @Override
    public R spikeById(String goodsId) {
        return null;
    }

    @Override
    public R percentage() {
        List<PercentagelVo> s = goodsSeckillMapper.percentage();

        return R.restResult(s, HttpStatus.SUCCESS,"商品秒杀进度比查询成功");
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public R addCart(String uniquelyId, String goodsCode) {
        try {
            //获取当前登录人
            String s1 = redisTemplate.opsForValue().get(uniquelyId+"");
            //json转对象
            PatientInfo o = JSON.parseObject(s1,PatientInfo.class);
            //创建一个cart数组
            ArrayList<Cart> list = new ArrayList<>();
            //创建一个cart对象
            Cart cart = new Cart();
            //判断是否为空
            if (o!=null)
            {
                //查询出单个商品信息
                GoodsInfo g = goodsSeckillMapper.spikeById(goodsCode);
                String s = redisTemplate.opsForValue().get(o.getPatientId()+"");
                List<Cart> o1 = JSON.parseArray(s, Cart.class);
                //判断是否有当前商品
                if(o1!=null)
                {
                    for (int i = 0; i < o1.size(); i++) {
                        Cart cart1 = o1.get(i);
                        String goodsName = cart1.getGoodsName();
                        if (goodsName.equals(g.getGoodsName())){
                            cart1.setGoodsCount(cart1.getGoodsCount()+1);
                            return R.restResult(null,HttpStatus.SUCCESS,"添加购物车成功");
                        }
                    }
                    //存储商品信息到购物车
                    cart.setGoodsCode(g.getGoodsCode());
                    cart.setGoodsCount(1);
                    cart.setGoodsName(g.getGoodsName());
                    cart.setGoodsPrice(g.getGoodsPrice());
                    o1.add(cart);
                    String s2 = String.valueOf(o1);
                    redisTemplate.opsForValue().set(o.getPatientId()+"",s2);
                    return R.restResult(null,HttpStatus.SUCCESS,"添加购物车成功");
                }
                //存储商品信息到购物车
                cart.setGoodsCode(g.getGoodsCode());
                cart.setGoodsCount(1);
                cart.setGoodsName(g.getGoodsName());
                cart.setGoodsPrice(g.getGoodsPrice());
                list.add(cart);
                String s2 = String.valueOf(list);
                redisTemplate.opsForValue().set(o.getPatientId()+"",s2);
                return R.restResult(null,HttpStatus.SUCCESS,"添加购物车成功");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.restResult(null,HttpStatus.ERROR,"添加购物车失败请联系管理员");
        }
        return null;
    }


}
